package org.scc.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.scc.dao.MeetingPlanDao;
import org.scc.dao.UserDao;
import org.scc.model.MeetingPlace;
import org.scc.model.MeetingPlan;
import org.scc.model.User;

public class MeetingPlanService {
	private MeetingPlanDao meetingPlanDao;
	private UserDao userDao;
	public MeetingPlanService(){
		meetingPlanDao = new MeetingPlanDao();
		userDao = new UserDao();
	}
	
	public List<User> findMeetingPlanUser(String date, int type, int meetingPlaceId){
		List<User> users = new ArrayList<User>();
		List<Integer> userIds = meetingPlanDao.getMeetingPlans(date, type, meetingPlaceId);
		for(int id : userIds){
			users.add(userDao.load(id));
		}
		return users;
	}
	
	public void addMeetingPlan(String date, int type, int meetingPlaceId, String userIds) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		MeetingPlan mp = new MeetingPlan();
		mp.setDate(sdf.parse(date));
		mp.setType(type);
		MeetingPlace mplace = new MeetingPlace();
		mplace.setId(meetingPlaceId);
		mp.setMeetingPlace(mplace);
		
		User u = new User();
		String[] ids = userIds.split(",");
		for(String uid : ids){
			u.setId(Integer.parseInt(uid));
			mp.setUser(u);
			meetingPlanDao.add(mp);
		}
	}
	
	public void deletePlan(String date, int type, int meetingPlaceId){
		meetingPlanDao.deletePlan(date, type, meetingPlaceId);
	}
}
